﻿using Archives.DbHelper;
using Archives.IService;
using Archives.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Archives.Controllers
{
    public class ReadOrDoController : Controller
    {
        /// <summary>
        /// 获取阅件编号信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetReadNumber(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return null;
            }
            var year = Convert.ToString(DateTime.Now.Year);
            var sql = string.Format(@"select * from GW_Read where Name = '{0}'", name);
            var readInfo = ModelConvertHelper<GW_Read>.ConvertToModel(SqlHelper.ExcuteDataQuery(sql)).FirstOrDefault();
            if (readInfo != null)
            {
                if (year.Contains(Convert.ToString(readInfo.Year)))
                {
                    var num = string.Empty;
                    if (readInfo.Number < 10)
                    {
                        num = "000";
                    }
                    else if (readInfo.Number < 100)
                    {
                        num = "00";
                    }
                    else if (readInfo.Number < 1000)
                    {
                        num = "0";
                    }
                    return readInfo.ThePrefix + Convert.ToString(readInfo.Year) + num + Convert.ToString(readInfo.Number);
                }
                else
                {
                    readInfo.Year += 1;
                    readInfo.Number = 1;
                    var updateSql = string.Format(@"update GW_Read set Year = {0},Number = {1},where Name ='{2}", readInfo.Year, readInfo.Number, name);
                    SqlHelper.ExcuteNonQuery(updateSql, null);
                    return readInfo.ThePrefix + Convert.ToString(readInfo.Year) + "000" + Convert.ToString(readInfo.Number);
                }
            }
            return "";
        }

        /// <summary>
        /// 获取办件编号信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetDoNumber(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return null;
            }
            var year = Convert.ToString(DateTime.Now.Year);
            var sql = string.Format(@"select * from GW_Do where Name = '{0}'", name);
            var doInfo = ModelConvertHelper<GW_Do>.ConvertToModel(SqlHelper.ExcuteDataQuery(sql)).FirstOrDefault();
            if (doInfo != null)
            {
                if (year.Contains(Convert.ToString(doInfo.Year)))
                {
                    var num = string.Empty;
                    if (doInfo.Number < 10)
                    {
                        num = "000";
                    }
                    else if (doInfo.Number < 100)
                    {
                        num = "00";
                    }
                    else if (doInfo.Number < 1000)
                    {
                        num = "0";
                    }
                    return Convert.ToString(doInfo.Year) + num + Convert.ToString(doInfo.Number);
                }
                else
                {
                    doInfo.Year += 1;
                    doInfo.Number = 1;
                    var updateSql = string.Format(@"update GW_Do set Year = {0},Number = {1},where Name ='{2}", doInfo.Year, doInfo.Number, name);
                    SqlHelper.ExcuteNonQuery(updateSql,null);
                    return Convert.ToString(doInfo.Year) + "000" + Convert.ToString(doInfo.Number);
                }
            }
            return "";
        }
    }
}